const { app,BrowserWindow,ipcMain} = require('electron');
const path = require('path');

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  });

  // and load the index.html of the app. 装在页面
  mainWindow.loadFile(path.join(__dirname, 'index.html'));

  // Open the DevTools.    打开开发者工具
  mainWindow.webContents.openDevTools();
 //任务栏中的图标叠加
  //mainWindow.setOverlayIcon('img/lover.png', 'Description for overlay')
  //闪烁框
 /* mainWindow.once('focus', () => mainWindow.flashFrame(false))
  mainWindow.flashFrame(true)*/

 //缩略图工具栏
 /* mainWindow.setThumbarButtons([
    {
      tooltip: 'button1',
      icon: path.join(__dirname, 'img/lover.png'),
      click () { console.log('button1 clicked') }
    }, {
      tooltip: 'button2',
      icon: path.join(__dirname, 'img/lover.png'),
      flags: ['enabled', 'dismissonclick'],
      click () { console.log('button2 clicked.') }
    }
  ])*/


  //进程通信
  require('./main/ipcMain.js');
  //执行设置菜单操作
   require('./utils/menu.js');
};
//全局快捷键  注意 引入的地方
require('./main/globalShortcut.js');

// 解决 CSP安全警告
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  //在 macOS 上，除非用户用 Cmd + Q 确定地退出，否则绝大部分应用及其菜单栏会保持激活。
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // 在macOS上，当单击dock图标并且没有其他窗口打开时， 通常在应用程序中重新创建一个窗口。
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});
//在线状态监测
let onlineStatusWindow
//渲染进程
/*app.whenReady().then(() => {
  onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false })
  onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
})*/
//主进程
app.whenReady().then(() => {
  onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false,
      webPreferences: {
        nodeIntegration: true,
     }
  })
  onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
})

ipcMain.on('online-status-changed', (event, status) => {
  console.log("主进程获取-OnlineStatus：",status)
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
